#if defined(windows_x86)
#include <windows.h>
#include <stdio.h>

//// 这就是按需加载的dll的主函数，dll被加载、卸载时，系统都回调用这个函数，通过dwReason判断
//extern "C" BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpvReserved)
//{
//    switch (dwReason)
//    {
//    // 如果是进程加载本dll
//    case DLL_PROCESS_ATTACH:
//        printf("process attach/n");
//        break;
//        // 如果是进程卸载本dll
//    case DLL_PROCESS_DETACH:
//        printf("thread attach/n");
//        break;
//        // 如果是线程加载本dll
//    case DLL_THREAD_ATTACH:
//        printf("thread attach/n");
//        break;
//        // 如果是线程卸载本dll
//    case DLL_THREAD_DETACH:
//        printf("process attach/n");
//        break;
//    }
//    // 如果返回FALSE，则说明加载失败，不会继续被加载，也不可使用
//    return TRUE;
//}

#else
#include <Arduino.h>
#include "STM32FreeRTOS.h"
#include <hwboard.h>
#ifdef LWIP
#include <lwip/apps/httpd.h>
#endif
#include <core_debug.h>
#include <uart.h>
#include "plc.h"
#include <stdlib.h>
#ifdef PLC

extern "C"
{
    void uart_debug_init(void);
    void phy_dump();
    osThreadId defaultTaskHandle;
    int last;
    void StartDefaultTask(void const *arg)
    {
        board.baseinit();
        board.netInit();
        startPLC();
        plc_set_state(Started);
        // ch.clear();
        vTaskDelete(NULL);
    }
#ifdef TRACESNIFFER
#include <sniffImport.h>
#endif
    void setup()
    {
        uart_debug_init();

        Serial1.begin(115200);
        // core_debug("\n%s\n", PLC_ID);

        osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 512);
        defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
        plc_set_state(Started);
#ifdef TRC_USE_TRACEALYZER_RECORDER
        vTraceEnable(TRC_START);
#endif
#ifdef TRACESNIFFER
        initialiseTraceSniffer();
#endif
        // vTraceStart();
        osKernelStart();
        core_debug("Insufficient RAM");
        while (1)
            ;
    }
#ifdef NTPClient
#include <NTPClient.h>
#endif
    void loop()
    {
#ifdef NTPClient
        NTPClient timeClient(ntpUDP);
        timeClient.begin();
        timeClient.update();

#endif
#if 0
        UBaseType_t ArraySize;
        TaskStatus_t *StatusArray;
        uint8_t x;
        uint32_t FreeRTOSRunTimeTicks;
        ArraySize = uxTaskGetNumberOfTasks();
        StatusArray = (TaskStatus_t *)pvPortMalloc(ArraySize * sizeof(TaskStatus_t));
        // while (1)
        // {

        if (StatusArray != NULL)
        {

            ArraySize = uxTaskGetSystemState((TaskStatus_t *)StatusArray,
                                             (UBaseType_t)ArraySize,
                                             (uint32_t *)&FreeRTOSRunTimeTicks);

            printf("TaskName\tPriority\tTaskNumber\tMinStk\t\n");
            for (x = 0; x < ArraySize; x++)
            {

                printf("%s\t%d\t%d\t%d\t%d\r\n",
                       StatusArray[x].pcTaskName,
                       (int)StatusArray[x].uxCurrentPriority,
                       (int)StatusArray[x].xTaskNumber,
                       (int)StatusArray[x].usStackHighWaterMark,
                       (int)((float)StatusArray[x].ulRunTimeCounter / FreeRTOSRunTimeTicks * 100));
            }
            printf("\n\n");
        }
        //     vTaskDelay(2000);
        // }
        vPortFree(StatusArray);
#endif
    }


}

#endif

#endif
